package core;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class InfoCorpus {
	
	private String path;
	
	public InfoCorpus(String path){
		this.path=path;
	}
	
	public int countWords() {
		int total=0;
		
		File file = new File(path);
		if(file.isDirectory()){
			File[] files = file.listFiles();
			for(File f : files){
				try {
					total+=calculateWords(f);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		else{
			try {
				total = calculateWords(file);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return total;
		
	}
	
	private int calculateWords(File file) throws IOException{
		int numberWords=0;
		BufferedReader br = new BufferedReader(new FileReader(file));
		String linea = br.readLine();
		while(linea!=null){
			
			String[] words = linea.split(" ");
			numberWords+=words.length;
			linea = br.readLine();
		}
		
		br.close();
		
		return numberWords;
	}
	
	public int countSentences(){
		int total=0;
		
		File file = new File(path);
		if(file.isDirectory()){
			File[] files = file.listFiles();
			for(File f : files){
				try {
					total+=calculateSentences(f);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		else{
			try {
				total = calculateSentences(file);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return total;
		
	}
	
	private int calculateSentences(File file) throws IOException{
		int numberSentences = 0;
		
		BufferedReader br = new BufferedReader(new FileReader(file));
		String linea = br.readLine();
		while(linea!=null){
			
			String[] words = linea.split(".");
			numberSentences+=words.length;
			linea = br.readLine();
		}
		
		br.close();
		
		return numberSentences;
	}

}
